package com.ruoyi.web.controller.safety;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.control.domain.RyssHistory;
import com.ruoyi.position.domain.Ryss;
import com.ruoyi.safety.dto.MarkDto;
import com.ruoyi.safety.vo.MarkVo;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safety.domain.Mark;
import com.ruoyi.safety.service.IMarkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cesiumController
 *
 * @author ruoyi
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/system/mark")
public class MarkController extends BaseController {
    @Autowired
    private IMarkService markService;

    /**
     * 查询cesium列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Mark mark) {
        //筛选del_flag  0
        startPage();
        List<Mark> list = markService.selectAll(mark);

//        List<Mark> collect = list.stream().filter
//                (markInList ->
//                        "0".equals(markInList.getDelFlag())
//                ).collect(Collectors.toList());


//        startPage();
//        List<Mark> list = markService.selectMarkList(mark);
        return getDataTable(list);
    }

    @GetMapping("/getListByType")
    public TableDataInfo getListByType() {
        List<Mark> list = markService.getListByType();
        return getDataTable(list);
    }

    @PostMapping("/getCheckBoxList")
    public TableDataInfo getCheckBoxList(@RequestBody String idsStr) {
        JSONObject jsonObject = new JSONObject(idsStr);
        JSONArray idsArray = jsonObject.getJSONArray("ids");
        int length = idsArray.length();
        String[] ids = new String[length];
        for (int i = 0; i < length; i++) {
            ids[i] = idsArray.getString(i);
        }
        //这里的ids  是指  point 的 delflag
        //List<String> ids = Arrays.asList(idsStr.split(","));
        List<Mark> list = markService.getCheckBoxList(ids);
        return getDataTable(list);
    }

    /**
     * 导出cesium列表
     */
    @PreAuthorize("@ss.hasPermi('system:mark:export')")
    @Log(title = "cesium", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Mark mark) {
        List<Mark> list = markService.selectMarkList(mark);
        ExcelUtil<Mark> util = new ExcelUtil<Mark>(Mark.class);
        util.exportExcel(response, list, "cesium数据");
    }

    /**
     * 获取cesium详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mark:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(markService.selectMarkById(id));
    }

    /**
     * 新增cesium
     */
    @PreAuthorize("@ss.hasPermi('system:mark:add')")
    @Log(title = "cesium", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Mark mark) {
        return toAjax(markService.insertMark(mark));
    }

    /**
     * 修改cesium
     */
    @PreAuthorize("@ss.hasPermi('system:mark:edit')")
    @Log(title = "cesium", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Mark mark) {
        return toAjax(markService.updateMark(mark));
    }

    /**
     * 删除cesium
     */
    @PreAuthorize("@ss.hasPermi('system:mark:remove')")
    @Log(title = "cesium", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(markService.deleteMarkByIds(ids));
    }


    //cesium    新增点位    经度 纬度
    @PostMapping("/addPoint")
    public AjaxResult addPoint(@RequestBody Mark mark) {
        markService.insertMark(mark);
        return success("200");
    }


    //清除全部标记信息   修改del_flag
    @PutMapping("/deleteAllPoint")
    public AjaxResult deleteAllPoint(@RequestBody MarkVo vo) {
        List<String> pointsIds = vo.getPointsIds();

        pointsIds.stream().forEach(id -> {
            Mark mark = markService.selectMarkById(Long.valueOf(id));
            mark.setDelFlag("1");
            markService.updateMark(mark);
        });

        return success("200");
    }

    /**
     * 根据类型查询标记列表
     */
    @PostMapping("/listByType")
    public AjaxResult listByType(@RequestBody Mark mark) {
        List<Mark> list = markService.listByType(mark.getMarkType());
        return success(list);
    }

    @PostMapping("/listByExtend2")
    public AjaxResult listByExtend2(@RequestBody MarkVo mark) {

        List<Mark> list = markService.listByExtend2(mark.getExtend2());
        return success(list);
    }

    @PostMapping("/listByExtend1")
    public AjaxResult listByExtend3(@RequestBody MarkVo mark) {

        List<Mark> list = markService.listByExtend1(mark.getExtend1());
        return success(list);
    }

    @PostMapping("/listByValue2")
    public AjaxResult listByValue2(@RequestBody MarkVo mark) {
        String value2 = mark.getValue2();
        System.out.println(value2);
        List<Mark> list = markService.listByValue2(mark.getValue2());
        return success(list);
    }


    /**
     * 根据id修改name
     */
    @PostMapping("/markNameById")
    public AjaxResult markNameById(@RequestBody Mark mark) {
        Long markId = mark.getId();

        // 判断 ID 是否为空
        if (markId != null) {
            markService.updateNameById(mark);
            // 返回成功响应
            return AjaxResult.success("Mark 信息更新成功");
        } else {
            // ID 为空，返回错误响应
            return AjaxResult.error("ID 不能为空");
        }
    }


    //点位关联设备摄像头
    @PostMapping("/deviceToPoint")
    public AjaxResult deviceToPoint(@RequestBody MarkVo vo) {

        markService.deviceToPoint(vo);
        return success("200");
    }


    //点位关联分站
    @PostMapping("/pointToPoint")
    public AjaxResult pointToPoint(@RequestBody MarkVo vo) {
        System.out.println(vo);
        markService.pointToPoint(vo);
        return success("200");
    }


    //分站--测点关系列表
    @PostMapping("/getSubPointList")
    public List<MarkDto> getSubPointList(@RequestBody MarkVo vo) {
        List<MarkDto> dtoList = markService.getSubPointList(vo);
        return dtoList;
    }

    //点位关联矿压测点
    @PostMapping("/pointToKyPoint")
    public AjaxResult pointToKyPoint(@RequestBody MarkVo vo) {
        markService.pointToKyPoint(vo);
        return success("200");
    }

    //点位关联水文分站
    @PostMapping("/pointToSwSub")
    public AjaxResult pointToSwSub(@RequestBody MarkVo vo) {
        markService.pointToSwSub(vo);
        return success("200");
    }


    //点位关联人员分站
    @PostMapping("/pointToPersonSub")
    public AjaxResult pointToPersonSub(@RequestBody MarkVo vo) {
        markService.pointToPersonSub(vo);
        return success("200");
    }


    //点位关联束管测点
    @PostMapping("/pointToSg")
    public AjaxResult pointToSg(@RequestBody MarkVo vo) {
        markService.pointToSg(vo);
        return success("200");
    }


    @GetMapping("/personList")
    public TableDataInfo personList(Mark mark) {
        //筛选del_flag  0
        startPage();
        List<Mark> list = markService.selectPersonList(mark);

        return getDataTable(list);
    }

    @GetMapping("/swList")
    public TableDataInfo swList(Mark mark) {
        //筛选del_flag  1
        startPage();
        List<Mark> list = markService.selectSwList(mark);

        return getDataTable(list);
    }

    @GetMapping("/kyList")
    public TableDataInfo kyList(Mark mark) {
        //筛选del_flag  2
        startPage();
        List<Mark> list = markService.selectKyList(mark);

        return getDataTable(list);
    }


    /**
     * 查询人员基站下人员详细信息
     */
    @PostMapping("/getSubstationPersonDetails")
    public List<Ryss> getSubstationPersonDetails(@RequestBody Mark mark) {
        List<Ryss> list = markService.getSubstationPersonDetails(mark);
        return list;
    }

}

